<?php
namespace app\api\controller\v1;
use app\common\controller\Api;
use app\admin\model\Credits as CreditModel;

class CreditDetail extends api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function getCreditList()
    {
        // 获取当前登录用户ID，具体方法可能需要根据您的认证系统调整
        $user = $this->auth->getUser();
        $userId=$user['uid'];
        if (!$userId) {
            return json(['code' => 0, 'msg' => '用户未登录', 'data' => null]);
        }

        // 获取分页参数，如果没有则使用默认值
        $page = input('page', 1);
        $limit = input('limit', 10);

        // 查询该用户的积分记录
        $credits = CreditModel::where('user_id', $userId)
            ->order('create_time', 'desc')
            ->page($page, $limit)
            ->select();

        // 获取总记录数
        $total = CreditModel::where('user_id', $userId)->count();

        return json([
            'code' => 200,
            'msg' => 'success',
            'data' => $credits,
            'total' => $total,
            'current_page' => $page,
            'per_page' => $limit
        ]);
    }

    public function getCreditInfo()
    {
        $creditId = input('param.creditId');
        $credit = CreditModel::where('id', $creditId)->find();
        return json($credit);
    }
}